/** 向量坐标转换相关工具类 */
export default class VectorUtil {
    /** 单例模式 */
    private static _instance: VectorUtil = new VectorUtil();
    private constructor() { }
    public static get _ins() {
        return this._instance;
    }

    /** 根据两个点  求角度 */
    public getAngleForPos(pos1: cc.Vec2, pos2: cc.Vec2, isVertical = false): number {
        let rad = 0;
        if (isVertical) {
            rad = -Math.atan2(pos2.x - pos1.x, pos2.y - pos1.y);
        } else {
            rad = Math.atan2(pos2.y - pos1.y, pos2.x - pos1.x);
        }
        return cc.misc.radiansToDegrees(rad);
    }
    /** 获取两个坐标之间的距离 */
    public getDistance(pos1: cc.Vec2, pos2: cc.Vec2): number {
        return pos1.sub(pos2).mag();
    };
    /** 根据一个角度和长度 计算相对应的坐标 */
    public getPosForAngleLen(angle: number, len: number, startPos: cc.Vec2 = cc.v2(0, 0)) {
        let rad = cc.misc.degreesToRadians(angle);
        return cc.v3(startPos.x + Math.cos(rad) * len, startPos.y + Math.sin(rad) * len);
    }
    /** 获取节点在另一个节点下的坐标 */
    public getToNodePosForNode(obj: cc.Node, mainObj: cc.Node): cc.Vec2 {
        let worldPos = obj.parent.convertToWorldSpaceAR(obj.position);
        let nodePos = mainObj.convertToNodeSpaceAR(worldPos)
        return cc.v2(nodePos.x, nodePos.y);
    };
    /** 获取节点的世界坐标 */
    public getToWorldPosAR(obj: cc.Node) {
        return obj.parent.convertToWorldSpaceAR(obj.position);
    }
    /** 通过世界坐标 获取相对节点的坐标 */
    public getToNodePosForWorld(worldPos: cc.Vec2, obj: cc.Node) {
        return obj.convertToNodeSpaceAR(worldPos);
    }
}